﻿using Api.Model;
using Microsoft.IdentityModel.Tokens;

/**
id：评论id，数据类型为int
ownerId：发评人id，数据类型为int
text：评论内容，数据类型为string
time：发评时间，数据类型为datatime
postId：帖子id，数据类型为int
**/

namespace Api.Dal
{
    public class CommentDAL
    {
        public static int GetCount()
        {
            int count = 0;
            EfDemoContext ef = new EfDemoContext();
            if (ef.Comments.IsNullOrEmpty())
            {
                return 0;
            }
            count = ef.Comments.Select(c => c.id).Max();
            return count;
        }

        public static Comment getCommentById(int id)
        {
            EfDemoContext efDemoContext = new EfDemoContext();
            Comment? com = efDemoContext.Comments.Where(c => c.id == id).FirstOrDefault();
            return com;
        }

        public static List<Comment> getComment (Comment comment)
        {
            EfDemoContext efDemoContext = new EfDemoContext();
            List<Comment> commentList = efDemoContext.Comments.Where(c =>
            (comment.id == -1 || c.id == comment.id) &&
            (comment.ownerId == -1 || c.ownerId == comment.ownerId) &&
            (comment.text == "N/A" || string.Equals(c.text, comment.text)) &&
            (comment.time == DateTime.MinValue || c.time == comment.time) &&
            (comment.postId == -1 || c.postId == comment.postId)
            ).OrderBy(c => c.time).ToList();
            return commentList;
        }

        public static int InsertComment(Comment comment)
        {
            if (comment != null)
            {
                EfDemoContext ef = new EfDemoContext();
                ef.Comments.Add(comment);

                //post的评论数+1
                Post p = PostDAL.getPostById(comment.postId);
                p.commentNum++;
                PostDAL.UpdatePost(p);

                ef.SaveChanges();
                return 1;
            }
            return -2;
        }

        public static bool DeleteCommentById(int commentId)
        {
            Comment? comment = getCommentById(commentId);
            if (comment != null)
            {
                EfDemoContext ef = new EfDemoContext();
                ef.Comments.Remove(comment);
                ef.SaveChanges();
                return true;
            }
            return false;
        }

        public static bool DeleteCommentByPostId(int commentPostId)
        {
            EfDemoContext efDemoContext = new EfDemoContext();
            List<Comment> commentListToDelete = efDemoContext.Comments.Where(c => c.postId == commentPostId).ToList();
            foreach (Comment comment in commentListToDelete)
            {
                efDemoContext.Comments.Remove(comment);
                efDemoContext.SaveChanges();
            }
            return true;
        }

        public static bool UpdateComment(Comment c)
        {
            Comment? comment = getCommentById(c.id);
            if (comment != null)
            {
                EfDemoContext ef = new EfDemoContext();
                c.ownerId = (c.ownerId != -1) ? c.ownerId : comment.ownerId;
                c.text = c.text != null ? c.text : comment.text;
                c.time = c.time != DateTime.MinValue ? c.time : comment.time;
                c.postId = c.postId != -1 ? c.postId : comment.postId;
                ef.Comments.Update(c);
                ef.SaveChanges();
                return true;
            }
            return false;
        }
    }
}
